Sorting Q and A
Write Time complexity for sorting algorithms
Answer:
Sorting | Time Complexity (Worst case) | Verdicts |
---|---|---|
Bubble Sort | O(n2) | Not Good |
Insertion Sort | O(n2) | Good for small dataset|
Selection Sort | O(n2) | For small dataset |
Merge Sort | O(nlong(n)) | Stable sort |
Quick Sort | O(nlong(n)) | Works good for general purpose |
Heap Sort | O(nlong(n)) | Good |
What are comparison sort and non-comparison sort?
Answer: Comparison sort algorithms determine the order of elements by comparing them using relational operators. Insertion sort, bubble sort, selection sort, merge sort, quick sort, heap sort are comparison sorts. Non-comparision sort algorithms donot compare elements directly, it uses properties of data to sort elements. Radix sort is non-comparison sort.
How quick sort works?
Answer: QuickSort is a divide-and-conquer sorting algorithm that works by recursively partitioning an array around a pivot element.
How merge sort works?
Answer: Merge Sort is a divide-and-conquer sorting algorithm that recursively splits an array into smaller subarrays (upto array of one element), sorts them, and then merges them back together.
How Insertion sort works?
Answer: Insertion Sort is a simple, in-place comparison-based sorting algorithm that builds the final sorted array one element at a time. It is efficient for small datasets and nearly sorted data, with a best-case time complexity of O(n).
How Selection Sort Works?
Answer: Selection Sort works by repeatedly finding the minimum (or maximum) element from the unsorted part of the array and putting it at the beginning (or at the end).
How heap sort works?
Answer: Heap Sort is a comparison-based sorting algorithm that uses a binary heap data structure to sort elements. It has O(n log n) time complexity in all cases and O(1) auxiliary space (in-place), making it efficient for large datasets.
What is a heap tree?
Answer: A heap tree (or simply heap) is a specialized complete binary tree that satisfies the heap property. It is commonly used to implement priority queues and forms the basis for the Heap Sort algorithm.
How a heap can be represented as an array?
Answer: A heap is represented as an array where: